/**
 * bind to a stripe form, permits to post a stripes event from
 * any control that is not able to submit a form, with the correct
 * event been handle by a ActionBean
 * 
 * 
 */
AcantoForm = function(formName, debugMode, callbackFunction)
{	
	this.callback = callbackFunction;				
		
	/* event handled by this library */
	this.DELETE 		 = 'delete';
	this.EDIT   		 = 'edit';
	this.CREATE 		 = 'create';
	this.UPDATE 		 = 'update';
	this.CLONE  		 = 'clone';	
	this.BACK   		 = 'back';
	this.REFRESH   		 = 'refresh';
	this.NEW_ITEM 		 = 'newItem';
	this.REDIRECT_TO     = 'redirectTo';
	this.FORWARD_TO      = 'forwardTo';
	this.SIMPLE_FILTER 	 = 'simpleFilter';
	this.RESET_FILTER 	 = 'resetFilter';
	
	this.FILTER_NAME     = 'filterName';
	
	this.REDIRECT 	= '__acanto_redirect';
	this.FORWARD 	= '__acanto_forward';	
	
	this.EMPTY_VALUE = ' ';
	this.METHOD      = 'method';
	
	this.debugMode = false;
	
	if(debugMode != null)
		this.debugMode = debugMode;
		
	if(document.getElementById(formName) != null)
		this.theForm = document.getElementById(formName);		
	else if (document.getElementsByName(formName))	
	{
		var tags = document.getElementsByName(formName);
		if(tags.length > 1)
			alert('there are ' + tags.length + ' form with the same name : ' + formName);
		else if (tags.length == 0)
			alert('there is no form with name : ' + formName);
		else
		{
			this.theForm = tags[0];
		}
	}	
	
	this.divContainer = document.createElement('DIV');            
	this.theForm.appendChild(this.divContainer);
};

AcantoForm.prototype = 
{
	// {{{ setDebugMode(debugMode)
	/**
	 * Set debug mode for this class
	 * 	 
	 * @public	
	 */		
	setDebugMode : function(debugMode)
	{
		if(debugMode == true) {		
			this.debugMode = true;	
			alert('Debug mode enabled');
			console.log('Debug mode enabled');
		}	
	}	
	// }}}	
	,
	// {{{ postForwardEvent(stripesEvent, forwardUrl)
	postForwardEvent : function(stripesEvent, toUrl)
	{	      	
		if(this.debugMode)
		{
			alert('AcantoForm.postForwardEvent(' + this.__formatArguments(arguments) + ')');
		}
			
		var postingEvent = stripesEvent;
		
		this.divContainer.innerHTML = '';
		
		this.__createEventSelector(postingEvent);	
		
		this.__setupForm( toUrl , 'POST' );
	
		if (arguments.length > 2)
			for (var i = 2; i < arguments.length; i++) 
		  	{	    	
		  	    if (arguments[i].indexOf('=') > 0)
		  	    {
		  	    	x = arguments[i].indexOf('=');
		    		var name = arguments[i].substring(0, x).trim();
		    		var value = arguments[i].substring(x+1).trim();
		    		
		    		var paramInput = this.__createHidden(name, value);	    		
					this.divContainer.appendChild(paramInput);	
		    	}
		    	else
		    	{
		    		alert('postForwardEvent(' + this.__formatArguments(arguments) + ')' +
		    		      ' - Error evaluating arguments[' + i + '] :' + arguments[i]);
		    	}	    	    	
		  	}
	  
	  	this.__submit(postingEvent);
	}
	// }}}	
	,
	// {{{ postRedirectEvent(stripesEvent, redirectUrl)
	postRedirectEvent : function(stripesEvent, toUrl)
	{	      
		if(this.debugMode) {
			console.log('AcantoForm.postRedirectEvent(' + this.__formatArguments(arguments) + ')');
			alert('AcantoForm.postRedirectEvent(' + this.__formatArguments(arguments) + ')');
		}
			
		var postingEvent = stripesEvent;
		
		this.divContainer.innerHTML = '';
		
		this.__createEventSelector(postingEvent);	
		
		this.__createRedirectTarget(toUrl);	
		
		if (arguments.length > 2)
			for (var i = 2; i < arguments.length; i++) 
		  	{	    	
		  	    if (arguments[i].indexOf('=') > 0)
		  	    {
		    		x = arguments[i].indexOf('=');
		    		var name = arguments[i].substring(0, x).trim();
		    		var value = arguments[i].substring(x+1).trim();
		    		
		    		var paramInput = this.__createHidden(name, value);	    		
					this.divContainer.appendChild(paramInput);	
		    	}
		    	else
		    	{
		    		alert('postRedirectEvent(' + this.__formatArguments(arguments) + ')' +
		    		      ' - Error evaluating arguments[' + i + '] :' + arguments[i]);
		    	}	    	    	
		  	}
	  
	  	this.__submit(postingEvent);
	}
	// }}}	
	,
	// {{{ deleteItems()
	/**
	 * convenience method to post a delete event to an action.
	 * Usually the form has a list of check box that User select to 
	 * post the item id he wants delete
	 * 	 
	 * @public	
	 */	
	deleteItems : function(toUrl)
	{		
		if(this.debugMode)
		{
			alert('AcantoForm.deleteItems(' + this.__formatArguments(arguments) + ')');
		}
			
		var postingEvent = this.DELETE;
		
		this.divContainer.innerHTML = '';
		
		this.__createEventSelector(postingEvent);	
		
		if (arguments.length > 1)
			for (var i = 1; i < arguments.length; i++) 
		  	{	    	
		  	    if (arguments[i].indexOf('=') > 0)
		  	    {
		    		x = arguments[i].indexOf('=');
		    		var name = arguments[i].substring(0, x).trim();
		    		var value = arguments[i].substring(x+1).trim();
		    		
		    		var paramInput = this.__createHidden(name, value);	    		
					this.divContainer.appendChild(paramInput);	
		    	}
		    	else
		    	{
		    		alert('AcantoForm.delete(' + this.__formatArguments(arguments) + ')' +
		    		      ' - Error evaluating arguments[' + i + '] :' + arguments[i]);
		    	}	    	    	
		  	}
		
		this.__setupForm( 'delete.htm' , 'POST' );
	  
	  	this.__submit(postingEvent);
	}
	// }}}
	,
	// {{{ edit()
	/**
	 * convenience method to post a edit method event to a stripes action.
	 * 	 
	 * @public	
	 */
	edit : function(toUrl)
	{
		if(this.debugMode)
			alert('AcantoForm.edit(' + this.__formatArguments(arguments) + ')');
			
		var postingEvent = this.EDIT;
		
		this.divContainer.innerHTML = '';
		
		this.__createEventSelector(postingEvent);	
		
		if (arguments.length > 1)
		{
			for (var i = 1; i < arguments.length; i++) 
		  	{	    	
		  	    if (arguments[i].indexOf('=') > 0)
		  	    {
		    		x = arguments[i].indexOf('=');
		    		var name = arguments[i].substring(0, x).trim();
		    		var value = arguments[i].substring(x+1).trim();
		    		
		    		var paramInput = this.__createHidden(name, value);	    		
					this.divContainer.appendChild(paramInput);	
		    	}
		    	else
		    	{
		    		alert('edit(' + this.__formatArguments(arguments) + ')' +
		    		      ' - Error evaluating arguments[' + i + '] :' + arguments[i]);
		    	}	    	    	
		  	}
		}
		
		this.__setupForm( toUrl , 'POST' );
	  
	  	this.__submit(postingEvent);
	}
	// }}}
	,
	// {{{ create()
	create : function(toUrl)
	{	
		if(this.debugMode)
			alert('AcantoForm.create(' + this.__formatArguments(arguments) + ')');
			
		var postingEvent = this.CREATE;
		
		this.divContainer.innerHTML = '';
		
		this.__createEventSelector(postingEvent);	
		
		if (arguments.length > 1)
			for (var i = 1; i < arguments.length; i++) 
		  	{	    	
		  	    if (arguments[i].indexOf('=') > 0)
		  	    {
		  	    	x = arguments[i].indexOf('=');
		    		var name = arguments[i].substring(0, x).trim();
		    		var value = arguments[i].substring(x+1).trim();
		    		
		    		var paramInput = this.__createHidden(name, value);	    		
					this.divContainer.appendChild(paramInput);	
		    	}
		    	else
		    	{
		    		alert('postForwardEvent(' + this.__formatArguments(arguments) + ')' +
		    		      ' - Error evaluating arguments[' + i + '] :' + arguments[i]);
		    	}	    	    	
		  	}
	
		this.__setupForm( toUrl, 'POST' );
		
	  	this.__submit(postingEvent);
	}
	// }}}
	,
	// {{{ clone(toUrl)
	clone : function(toUrl)
	{	
		if(this.debugMode)
			alert('AcantoForm.clone(' + this.__formatArguments(arguments) + ')');
			
		var postingEvent = this.CLONE;
		
		this.divContainer.innerHTML = '';
	
		this.__createEventSelector(postingEvent);	
		
		this.__createForwardTarget(toUrl);	
		
		if (arguments.length > 1)
			for (var i = 1; i < arguments.length; i++) 
		  	{	    	
		  	    if (arguments[i].indexOf('=') > 0)
		  	    {
		  	    	x = arguments[i].indexOf('=');
		    		var name = arguments[i].substring(0, x).trim();
		    		var value = arguments[i].substring(x+1).trim();
		    		
		    		var paramInput = this.__createHidden(name, value);	    		
					this.divContainer.appendChild(paramInput);	
		    	}
		    	else
		    	{
		    		alert('postForwardEvent(' + this.__formatArguments(arguments) + ')' +
		    		      ' - Error evaluating arguments[' + i + '] :' + arguments[i]);
		    	}	    	    	
		  	}
	  
	  	this.__submit(postingEvent);
	}
	// }}}
	,
	// {{{ back()
	back : function(toUrl)
	{
		if(this.debugMode)
			alert('AcantoForm.back(' + this.__formatArguments(arguments) + ')');
			
		var postingEvent = this.BACK;
		
		this.divContainer.innerHTML = '';
			
		if(toUrl == null)
		{
			alert('error - AcantoForm.back() must have at list a url parameter');
			return;
		}		
		
		this.__createEventSelector(postingEvent);	
		
		if (arguments.length > 1)
			for (var i = 1; i < arguments.length; i++) 
		  	{	    	
		  	    if (arguments[i].indexOf('=') > 0)
		  	    {
		    		x = arguments[i].indexOf('=');
		    		var name = arguments[i].substring(0, x).trim();
		    		var value = arguments[i].substring(x+1).trim();
		    		
		    		var paramInput = this.__createHidden(name, value);	    		
					this.divContainer.appendChild(paramInput);	
		    	}
		    	else
		    	{
		    		alert('edit(' + this.__formatArguments(arguments) + ')' +
		    		      ' - Error evaluating arguments[' + i + '] :' + arguments[i]);
		    	}	    	    	
		  	}
		
		this.__setupForm( toUrl , 'GET' );
		
	  	this.__submit(postingEvent);
	}
	// }}}
	,
	// {{{ refresh()
	/**
	 *  refresh does not take any url params because does a simple post back with param
	 *  passed to this method
	 *
	 */
	refresh : function(toUrl)
	{	
		if(this.debugMode)
			alert('AcantoForm.refresh(' + this.__formatArguments(arguments) + ')');
			
		var postingEvent = this.REFRESH;
		
		this.divContainer.innerHTML = '';
	
		this.__createEventSelector(postingEvent);	
		
		if (arguments.length > 0)
		{
			for (var i = 0; i < arguments.length; i++) 
		  	{	    	
		  	    if (arguments[i].indexOf('=') > 0)
		  	    {
		    		x = arguments[i].indexOf('=');
		    		var name = arguments[i].substring(0, x).trim();
		    		var value = arguments[i].substring(x+1).trim();
		    		
		    		var paramInput = this.__createHidden(name, value);	    		
					this.divContainer.appendChild(paramInput);	
		    	}
		    	else
		    	{
		    		alert('refresh(' + this.__formatArguments(arguments) + ')' +
		    		      ' - Error evaluating arguments[' + i + '] :' + arguments[i]);
		    	}	    	    	
		  	}
		}
		
		this.__createRedirectTarget(toUrl);
	  
	  	this.__submit(postingEvent);
	}
	// }}}
	,
	// {{{ newItem()
	newItem : function(toUrl)
	{	
		if(this.debugMode)
			alert('AcantoForm.newItem(' + this.__formatArguments(arguments) + ')');
			
		var postingEvent = this.NEW_ITEM;
		
		this.divContainer.innerHTML = '';
			
		if(toUrl == null)
		{
			alert('error - AcantoForm.newItem() must have the url parameter');
			return;
		}	
	
		this.__createRedirectTarget(toUrl);
		
		if (arguments.length > 1)
		{
			for (var i = 1; i < arguments.length; i++) 
		  	{	    	
		  	    if (arguments[i].indexOf('=') > 0)
		  	    {
		    		x = arguments[i].indexOf('=');
		    		var name = arguments[i].substring(0, x).trim();
		    		var value = arguments[i].substring(x+1).trim();
		    		
		    		var paramInput = this.__createHidden(name, value);	    		
					this.divContainer.appendChild(paramInput);	
		    	}
		    	else
		    	{
		    		alert('edit(' + this.__formatArguments(arguments) + ')' +
		    		      ' - Error evaluating arguments[' + i + '] :' + arguments[i]);
		    	}	    	    	
		  	}
		}
		
	  	this.__submit(postingEvent);
	}
	//}}}
	,
	//{{{ __createRedirectTarget()
	__createRedirectTarget : function(toUrl)
	{	
		tmpUrl = (toUrl != null ? toUrl : this.EMPTY_VALUE);
		
		if( tmpUrl.indexOf('=') > 0 && tmpUrl.indexOf('=') < tmpUrl.indexOf('?'))
			alert('param [toUrl := ' + toUrl + '] has a = inside, probably wrong method call');
			
		actionInput = this.__createHidden(this.REDIRECT, tmpUrl);	          		      
		this.divContainer.appendChild(actionInput);
	}
	//}}}
	,
	// {{{ redirectTo()
	redirectTo : function(toUrl)
	{	
		if(this.debugMode) {
			console.log('AcantoForm.redirectTo(' + this.__formatArguments(arguments) + ')');
			alert('AcantoForm.redirectTo(' + this.__formatArguments(arguments) + ')');
		}
			
		var postingEvent = this.REDIRECT_TO;
		
		this.divContainer.innerHTML = '';
			
		if (arguments.length == 0)
		{
			alert('error - AcantoForm.redirectTo() must have at list a url parameter');
			return;
		}
		else if (arguments.length > 1)	
		{
			alert('error - AcantoForm.redirectTo() must have only one parameter,\n' +
				  'actual parameter count is :' + arguments.length + ',\n' + 
			      'insted consider using postRedirectEvent');
			return;
		}		
		
		this.__createEventSelector(postingEvent);	
		
		this.__createRedirectTarget(toUrl);	
						
	  	this.__submit(postingEvent);
	}
	,
	// {{{ forwardTo()
	forwardTo : function(toUrl)
	{	
		if(this.debugMode) {
			console.log('AcantoForm.forwardTo(' + this.__formatArguments(arguments) + ')');
			alert('AcantoForm.forwardTo(' + this.__formatArguments(arguments) + ')');
		}
			
		var postingEvent = this.FORWARD_TO;
		
		this.divContainer.innerHTML = '';
			
		if (arguments.length == 0)
		{
			alert('error - AcantoForm.forwardTo() must have at list a url parameter');
			return;
		}
		else if(arguments.length > 1)	
		{
			alert('error - AcantoForm.forwardTo() must have only one parameter,\n' +
				  'actual parameter count is :' + arguments.length + ',\n' + 
			      'insted consider using postRedirectEvent');
			return;return;
		}	
		
		this.__createEventSelector(postingEvent);	
		
		this.__createForwardTarget(toUrl);	
					
	  	this.__submit(postingEvent);
	}
	,
	// {{{ simpleFilter()
	simpleFilter : function()
	{	
		if(this.debugMode) {
			alert('AcantoForm.simpleFilter(' + this.__formatArguments(arguments) + ')');
		}
		
		if (document.getElementsByName(this.FILTER_NAME))	
		{
			var tags = document.getElementsByName(this.FILTER_NAME);
			if(tags.length > 1)
			{
				alert('there are ' + tags.length + ' input with the same name : ' + this.FILTER_NAME);
				return;
			}
			else if (tags.length == 0)
			{
				alert('there is no input with name : ' + this.FILTER_NAME);
				return;
			}
			else
			{
				filterName = tags[0];
				
				if(filterName.value == '')
				{
					alert('No filter specified!');
					return;
				}
			}
		}	
		
		var postingEvent = this.SIMPLE_FILTER;
		
		this.__setupForm( 'simpleFilter.htm', 'POST' );
		
		this.__submit(postingEvent);
		
	//	jQuery.get('simpleFilter.htm?filterName='+ filterName.value +'&filterValue=' + filterValue.value);
	}
	// }}}
	,
	// {{{ resetSimpleFilter()
	resetFilter : function()
	{	
		if(this.debugMode)
			alert('AcantoForm.resetFilter(' + this.__formatArguments(arguments) + ')');
			
		var postingEvent = this.RESET_FILTER;
		
		this.divContainer.innerHTML = '';	
		
		this.__setupForm( 'resetFilter.htm', 'GET' );
		
	  	this.__submit(postingEvent);
	}
	// }}}
	,
	// {{{ __createEventSelector()
	__createEventSelector : function(eventName)
	{	
		var input = this.__createHidden( this.METHOD, eventName);	          
		this.divContainer.appendChild(input);
	}
	// }}}
	,
	// {{{ __createEventSelector()
	__submit : function(stripesEvent)
	{	
		var invokeSubmit = true;
		
		if(this.callback != null && this.callback != undefined)	          
			invokeSubmit = this.callback(stripesEvent, this);
			
		if(invokeSubmit != false)
			this.theForm.submit();
	}
	// }}}
	,
	// {{{ __createRedirectTarget()
	__createRedirectTarget : function(toUrl)
	{	
		tmpUrl = (toUrl != null ? toUrl : this.EMPTY_VALUE);
		
		if( tmpUrl.indexOf('=') > 0 && tmpUrl.indexOf('=') < tmpUrl.indexOf('?'))
			alert('param [toUrl := ' + toUrl + '] has a = inside, probably wrong method call');
			
		actionInput = this.__createHidden(this.REDIRECT, tmpUrl);	          		      
		this.divContainer.appendChild(actionInput);
	}
	// }}}
	,
	//{{{ __setupForm()
	__setupForm : function(action, method)
	{	
		this.theForm.setAttribute ('action', action);  
		this.theForm.setAttribute ('method', method);
		
		if(this.debugMode) {
			console.log('__createForm : form (' + this.theForm + ')');
			alert('__createForm : input (' + this.theForm + ')');
		}
	}
	//}}}
	,
	// {{{ __createHidden()
	__createHidden : function(name, value)
	{	
		var input = document.createElement('INPUT');
		
		input.setAttribute ('type', 'hidden');  
		input.setAttribute ('id', name);
		input.setAttribute ('name', name);
		input.setAttribute ('value', value);    
		
		if(this.debugMode) {
			console.log('__createHidden : input (' + input + ')');
			alert('__createHidden : input (' + input + ')');
		}
		
		return input;   	
	}
	// }}}
	,
	// {{{ __formatArguments(arguments)
	__formatArguments : function(arguments)
	{
		var str = '';
		var elements = new Array();
	
	  	for (var i = 0; i < arguments.length; i++) 
	  		str = str + '\'' + arguments[i] + '\', ';        	
	  
	  	return str.substr(0,str.length - 2);
	}
	//}}}
	,
	//{{{ addGroups(idElement)
	addGroups : function(idElement) 
	{
		var id = document.getElementById(idElement).value;
		var idAttribute = 'itemId=' + id;
		frm.postForwardEvent('addgroups', 'editItem.htm', idAttribute);
	}
	//}}}	
	,
	//{{{ removeGroups(idElement)
	removeGroups : function(idElement) 
	{
		var id = document.getElementById(idElement).value;
		var idAttribute = 'itemId=' + id;
		frm.postForwardEvent('removegroups', 'editItem.htm', idAttribute);
	}
	//}}}	
};//AcantoForm.prototype